

$.extend(_$,{
    getTarget:function(target){
        if(target.nodeType){
            return $(target);
        }else if(isString(target)){
            return $(target);
        }
        return target;
    },
    getFunctoin:function(fn,scope){
        var func;
        if(scope){
             func = scope[fn];
        }else{
            func = window[fn];
        }
        return func;
    },
    css : function(target, a) {
        var j = a.toLowerCase().split(";");
        for (var f = 0, d = j.length; f < d; f++) {
            var k = j[f];
            var m = k.split(":");
            if (m.length > 1) {
                if (m.length > 2) {
                    var h = m[0].trim();
                    m.removeAt(0);
                    var g = m.join(":").trim();
                    target.css(h, g);
                } else {
                    target.css(m[0].trim(), m[1].trim());
                }
            }
        }
    }
});

/**
 js面向对象语法
 */
$.extend(_$,{
    extend:function(childClass, parentClass, props) {
        var childProps = childClass.prototype, parentProps = parentClass.prototype;
        if (childClass.superclass == parentProps) {
            return;
        }

        childClass.superclass = parentProps;
        childClass.superclass.constructor = parentClass;

        for ( var key in parentProps) {
            childProps[key] = parentProps[key];
        }
        if (props) {
            for ( var key1 in props) {
                childProps[key1] = props[key1];
            }
        }
        return childClass;
    }
});




$.extend(_$,{
    components : {},
    uids:{},
    classes:{},
    _clsPre:"cc-",
    _uiPreCls:"ui-",
    reg:function(obj){
        if(obj === null || !obj.isComponent){
            return;
        }
        var id = obj.id;
        _$.components[id] = obj;
        var uid = obj.uid;
        _$.uids[uid] = obj;
    },
    unReg:function(obj){
        if(obj === null || !obj.isComponent ){
            return;
        }
        var id = obj.id;
        delete _$.components[id];
    },
    getById:function(id) {
        var obj = _$.components[id];
        return isEmpty(obj)?null:obj;
    },
    getByUId:function(uid) {
        var obj = _$.uids[uid];
        return isEmpty(obj)?null:obj;
    },
    regClass:function(pluginName,func){
        _$.parser.plugins.add(pluginName);
        if(!_$.classes[pluginName]){
            func.prototype.type = pluginName;
            _$.classes[pluginName] = func;
        }
    },
    getClass:function(pluginName){
        var func = _$.classes[pluginName];
        return isEmpty(func)?null:func;
    },
    findControls:function(context){
        var _this = this;
        if(context === null) {
            context = $("body");
        }
        var list=$("[class^='"+_this._uiPreCls+"']",context);
        var items = [];
        list.each(function(e){
            var obj = $(this);
            var uid = obj.attr("componentId");
            obj = _$.getByUId(uid);
            items.add(obj);
        });
        return items;
    }
});


$.extend(_$,{
    parser:{
        parseOptions: function(target,properties){

            var options = {};
            var s = $.trim(target.attr('data-options'));
            if (s){
                if (s.substring(0, 1) != '{'){
                    s = '{' + s + '}';
                }
                options = (new Function('return ' + s))();
            }

            $.map(['width','height','minWidth','maxWidth','minHeight','maxHeight'], function(p){
                var pv = $.trim(target[0].style[p] || '');
                if (pv){
                    if (pv.indexOf('%') == -1){
                        pv = parseInt(pv);
                        if (isNaN(pv)){
                            pv = undefined;
                        }
                    }
                    options[p] = pv;
                }
            });

            if (properties){
                var opts = {};
                for(var i=0; i<properties.length; i++){
                    var pp = properties[i];
                    if (typeof pp == 'string'){
                        opts[pp] = target.attr(pp);
                    } else {
                        for(var name in pp){
                            var type = pp[name];
                            if (type == 'boolean'  && isNotEmpty(target.attr(name))){
                                var val = target[0].getAttribute(name);
                                opts[name] = val ? (val == 'true') : false;
                            } else if (type == 'number' && isNotEmpty(target.attr(name))){
                                opts[name] = target.attr(name)=='0' ? 0 : parseFloat(target.attr(name)) ;
                            }else if (type == 'array'  && isNotEmpty(target.attr(name))){
                                var str = target.attr(name);
                                str = str.replaceAll("'","\"");
                                opts[name] = JSON.parse(str);
                            }
                            else if (type == 'json'){
                                var s = $.trim(target.attr(name));
                                if (s){
                                    if (s.substring(0, 1) != '{'){
                                        s = '{' + s + '}';
                                    }
                                    options[name] = (new Function('return ' + s))();
                                }else{
                                    options[name]={};
                                }
                            }else if(type == 'function'){
                                var s = $.trim(target.attr(name));
                                if(isNotEmpty(s)){
                                    var func = _$.getFunctoin(s);
                                    options[name]=func;
                                }
                            }
                            else if (type == 'object'){
                                var s = $.trim(target.attr(name));
                                if (s){
                                    options[name]=eval('(' + s + ')');
                                }
                            }
                        }
                    }
                }
                $.extend(options, opts);
            }

            return options;
        },
        auto: true,
        ids:[],
        count:0,
        onComplete: function(){
            logger("------onComplete---------");
        },
        onComponentComplete: function(id,type){
            var _this = this;
            _this.ids.remove(id);
            logger("***控件["+type+"]件完成,id[" + id+"]");
            if(_this.ids.length === 0){
                logger("***控件解析完成");
                _this.onComplete.call(_this);
            }
        },
        plugins:[],
        parse: function(context){
            if(isEmpty(context)) {
                context = $("body");
            }else if(isString(context)){
                context = $(context);
            }else if(context.nodeType){
                context = $(context);
            }
            if(isEmpty(context) && context.size() == 0) {
                return;
            }
            var _this = this;
            var components = {};
            for(var i=0; i<_this.plugins.length; i++){
                var name = _this.plugins[i];
                var list = $('.' + _$._clsPre + name, context);
                list.each(function(e){
                    var obj = $(this);
                    var id = obj.attr("id");
                    _this.count++;
                    _this.ids.add(id);
                });
                components[name] = list;
            }
            if(_this.count === 0){
                _$.parser.onComplete.call(_this);
                return;
            }
            logger("控件数 " + _this.count);
            for(var componentName in components){
                var list1 = components[componentName];
                if(list1.size() === 0){
                    continue;
                }

                logger("控件名称["+componentName+"],数量 " + list1.size());
                if(list1 !== null && list1.size()>0){

                    list1.each(function(){
                        var target = $(this);
                        var Func =  _$.getClass(componentName);
                        if(Func !== null){
                            new Func(target);
                        }
                    });
                }
            }
        }
    }
});


